/**
 * @file 获取echart配置
 * @author fanxingguang
 */

// 默认显示的点数
const showPointCount = 70;

// MA计算公式
function calculateMA(dayCount, sdata) {
    let result = [];
    for (let i = 0, len = sdata.length; i < len; i++) {
        if (i < dayCount) {
            result.push('-');
            continue;
        }

        let sum = 0;
        for (let j = 0; j < dayCount; j++) {
            sum += sdata[i - j][2];
        }
        result.push((sum / dayCount).toFixed(2));
    }
    return result;
}

export default function getOption(param) {
    let startPoint = 0;
    let endPoint = 100;
    let defaultShowPointCount = 70;
    if (param.xdata.length > defaultShowPointCount) {
        startPoint = endPoint - (defaultShowPointCount / param.xdata.length).toFixed(4) * 100;
    }
    let tmpObj = {};
    param.pointData.forEach(item => {
        let date = item.xAxis;
        let style = item.itemStyle;
        item.itemStyle = {
            color: style.normal.color
        };
        item.label = {
            position: style.normal.label.position
        };

        if (tmpObj[date]) {
            if (tmpObj[date]) {
                item.label.distance = 20;
            }
        }
        else {
            tmpObj[date] = true;
        }
    });
    let option = {
        tooltip: {
            trigger: 'axis',
            formatter(params) {
                let {seriesName, name, value} = params[0];
                return `
                    ${seriesName} ${name}<br/>
                    开盘 : ${value[0]} 最高 : ${value[3]}<br/>
                    收盘 : ${value[1]} 最低 : ${value[2]}
                `;
            }
        },
        legend: {
            data: ['日K线', 'MA5', 'MA10', '止盈线', '止损线'],
            textStyle: {color: '#fff'}
        },
        grid: {
            left: '5%',
            right: '5%',
            bottom: '20%'
        },
        toolbox: {
            show: true,
            feature: {
                dataZoom: {
                    show: true
                },
                saveAsImage: {
                    show: true
                },
                myTool: {
                    show: true,
                    title: '全屏',
                    // icon: basePath +
                    //     'common/plugin/jquery-easyui/1.4.3/themes/icons/search.png',
                    onclick() {
                        // stockDisplay($('#tcode').val(), $('#tname').val());
                    }
                }
            }
        },
        dataZoom: {
            show: true,
            realtime: false,
            start: startPoint,
            end: endPoint,
            zoomLock: false,
            dataBackground: {
                areaStyle: {color: '#fff'}
            },
            textStyle: {color: '#fff'}
        },
        yAxis: [
            {
                type: 'value',
                scale: true,
                axisLabel: {
                    show: true,
                    color: 'rgba(203, 203, 203, 1)'
                },
                axisLine: {
                    lineStyle: {
                        color: '#fff'
                    }
                },
                splitLine: {
                    show: true,
                    lineStyle: {
                        color: ['#483d8b'],
                        type: 'dashed',
                        width: 1
                    }
                },
                boundaryGap: [0.05, 0.05]
            }
        ],
        xAxis: [
            {
                type: 'category',
                boundaryGap: true,
                axisLabel: {
                    show: true,
                    color: '#fff'
                },
                axisLine: {
                    lineStyle: {
                        color: '#fff' // 坐标轴线颜色
                    }
                },
                splitLine: {
                    show: false
                },
                axisTick: {
                    onGap: false
                },
                data: param.xdata
            }
        ],
        series: [
            {
                name: '日K线',
                type: 'k',
                data: param.sdata,
                markPoint: {
                    show: true,
                    symbolSize: 1,
                    label: {
                        formatter(param) {
                            if (param.value === '+') {
                                return '+';
                            }
                            else if (parseInt(param.value, 10) > 100) {
                                return parseInt(param.value, 10) - 100;
                            }
                            else {
                                return param.value;
                            }
                        }
                    },
                    data: param.pointData
                }
            },
            {
                name: 'MA5',
                type: 'line',
                data: calculateMA(5, param.sdata),
                smooth: true,
                lineStyle: {
                    normal: {
                        color: '#F7A71C',
                        opacity: 0.5
                    }
                }
            },
            {
                name: 'MA10',
                type: 'line',
                data: calculateMA(10, param.sdata),
                smooth: true,
                lineStyle: {
                    normal: {
                        color: '#F7A71C',
                        opacity: 0.5
                    }
                }
            },
            {
                name: '止盈线',
                type: 'line',
                data: [],
                markLine: {
                    itemStyle: {
                        normal: {
                            lineStyle: {type: 'dashed', color: 'green'},
                            label: {show: true, position: 'left'}
                        } // dashed,solid
                    },
                    symbol: ['none', 'none'],
                    data: param.tdstList
                }
            },
            {
                name: '止损线',
                type: 'line',
                data: [],
                markLine: {
                    itemStyle: {
                        normal: {
                            lineStyle: {type: 'solid', color: 'red'},
                            label: {show: true, position: 'left'}
                        }
                    },
                    symbol: ['none', 'none'],
                    data: param.stopList
                }
            }
        ]
    };
    return option;
}
